package cn.aib.eduservice.controller.front;

import cn.aib.commonutils.JwtUtils;
import cn.aib.commonutils.R;
import cn.aib.eduservice.client.OrderClient;
import cn.aib.eduservice.entity.chapter.ChapterVo;
import cn.aib.eduservice.entity.frontVo.CourseQueryVo;
import cn.aib.eduservice.entity.vo.CourseWebVo;
import cn.aib.eduservice.service.EduChapterService;
import cn.aib.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
//@CrossOrigin
public class EduCourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private OrderClient orderClient;
    /**
     *  课程列表分页查询
     *  方法名：getCourseFrontPageList
     *  参数：long current,long limit,条件对象
     *  返回值：R
     */
    @PostMapping("getCourseFrontList/{current}/{limit}")
    public R getCourseFrontPageList(@PathVariable long current, @PathVariable long limit,
                                    @RequestBody(required = false) CourseQueryVo courseQueryVo){
        Map<String,Object> coursePage = eduCourseService.selectFrontCoursePagelist(current,limit,courseQueryVo);

        return R.ok().data(coursePage);
    }

    /**
     *  课程列表分页查询
     *  方法名：getCourseFrontInfo
     *  参数：String id(课程id)
     *  返回值：R
     */
    @GetMapping("getCourseInfo/{id}")
    public R getCourseFrontInfo(@PathVariable String id, HttpServletRequest request){
        //查询课程信息
        CourseWebVo course = eduCourseService.getFrontCourseInfo(id);

        //查询章节和小结
        List<ChapterVo> chapterVosList = eduChapterService.getChapterVideoByCourseId(id);

        //查询用户是否支付
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Boolean isBuy = false;

        if(!StringUtils.isEmpty(memberId)){
            isBuy = orderClient.isOrderByPay(id, memberId);
        }

        //返回
        return R.ok().data("course",course).data("chapterList",chapterVosList).data("isBuy", isBuy?1:0);
    }
}
